/*
 * @Author: gyj 
 * @Date: 2018-02-06 16:43:56 
 * @Last Modified by: softLink.gyj
 * @Last Modified time: 2018-02-07 14:46:40
 */

class Paging {
    constructor(el,options) {
        this.el = el;
        this.options = {
            pageNow: 1, //当前页码
            totalPage: 0, //总页数
            totalSize: 0, //数据总数
            callBack: ()=>{}, //回调函数
            firstPage:'首页',
            lastPage:'尾页',
        }
        this.options = Object.assign(this.options,options);

        //初始化分页
        this.init();
    }

    init() {
        this.creatHtml();
        this.bindEvent();
    }

    creatHtml() {
        let content = '';
        let current = this.options.pageNow;
        let total = this.options.totalPage;
        let totalNum = this.options.totalSize;

        content += "<a id=\"firstPage\">"+ this.options.firstPage +"</a><a id='prePage'> <i class='iconfont icon-leftarrow'></i></a>";

        // 总页数大于6时
        if (total > 6) {
            //当前页数小于5时显示省略号
            if (current < 5) {
                for (let i = 1; i < 6; i++) {
                    if (current == i) {
                        content += "<a class='current'>" + i + "</a>";
                    } else {
                        content += "<a>" + i + "</a>";
                    }
                }
                content += ". . .";
                content += "<a>" + total + "</a>";
            } else {
                //页码在中间部分时候	
                if (current < total - 3) {
                    for (let i = current - 2; i < current + 3; i++) {
                        if (current == i) {
                            content += "<a class='current'>" + i + "</a>";
                        } else {
                            content += "<a>" + i + "</a>";
                        }
                    }
                    content += ". . .";
                    content += "<a>" + total + "</a>";
                    //判断页码在末尾的时候
                } else {
                    content += "<a>1</a>";
                    content += ". . .";
                    for (let i = total - 4; i < total + 1; i++) {
                        if (current == i) {
                            content += "<a class='current'>" + i + "</a>";
                        } else {
                            content += "<a>" + i + "</a>";
                        }
                    }
                }
            }
            //页面总数小于6的时候
        } else {
            for (var i = 1; i < total + 1; i++) {
                if (current == i) {
                    content += "<a class='current'>" + i + "</a>";
                } else {
                    content += "<a>" + i + "</a>";
                }
            }
        }
        content += "<a id='nextPage'><i class='iconfont icon-rightarrow'></i></a>";
        content += "<a id=\"lastPage\">"+ this.options.lastPage +"</a>";
        content += "<span class='totalPages'> 共<span>" + total + "</span>页 </span>";
        content += "<span class='totalSize'> 共<span>" + totalNum + "</span>条记录 </span>";
        this.el.innerHTML = content;
    }

    bindEvent() {
        this.el.onclick = (event) => {
            event.preventDefault();
            if (event.target.tagName === 'A') {
                let element = event.path[0];
                this.pageAreaClick(element);
            } else {
                event.path.forEach((e) => {
                    if (e.tagName === 'A') {
                        let el = e;
                        this.pageAreaClick(el);
                    }
                })
            }
        }

       document.documentElement.onselectstart = e => false;
    }

    pageAreaClick(element) {
        let id = element.id;
        if (id === 'firstPage') {
            if (this.options.pageNow === 1) return;
            this.options.pageNow = 1;
        } else if (id === 'prePage') {
            if (this.options.pageNow === 1) return;
            this.options.pageNow -= 1;
        } else if (id === 'nextPage') {
            if (this.options.pageNow === this.options.totalPage) return;
            this.options.pageNow += 1;
        } else if (id === 'lastPage') {
            if (this.options.pageNow === this.options.totalPage) return;
            this.options.pageNow = this.options.totalPage;
        } else {
            let oldPage = this.options.pageNow;
            let now = parseFloat(element.innerHTML);
            if (oldPage === now) return;
            this.options.pageNow = now;
        }
        this.creatHtml();
        this.options.callBack instanceof Function && this.options.callBack(this.options.pageNow);
    }
}